<?php

namespace Services\Adshopuser;

use App\Adshopuser\Models\BShopGoodsCategory;
use App\Adshopuser\Models\BShopGoodsSku;
use App\Adshopuser\Models\BShopSkuModelKey;
use App\Adshopuser\Models\BShopSkuValue;
use App\Adshopuser\Models\BshopUsersInfo;
use Common\Common;
use App\Adshopuser\Models\BShopGoods;
use Common\Tree;
use Services\ServicesAdShopuserBase;

class ShopgoodsServices extends ServicesAdShopuserBase
{

    /**
     * 根据店铺id获取店铺分类和公用分类
     */
    public function getCategoryList($shop_id)
    {
        $BShopUsersInfoDb = new BshopUsersInfo();
        $bShopUsersInfo = $BShopUsersInfoDb->findFirstByShopId($shop_id);
        if (!$bShopUsersInfo) {
            return ['status' => false, 'msg' => '没有找到店铺入驻信息'];
        }
        $BShopGoodsCategoryDb = new BShopGoodsCategory();
        $findOneCategory = $BShopGoodsCategoryDb->findFirstById($bShopUsersInfo->category_id);
        if (!$findOneCategory) {
            return ['status' => false, 'msg' => '没有找到用户入驻时候选择的分类'];
        }
        $path = "{$findOneCategory->path}";
        $getList = $BShopGoodsCategoryDb->findShopPath($shop_id, $path);
        $tree = new Tree();
        $responseResult = $tree->list2tree($getList, 'value', 'pid', 'children');
        return ['status' => true, 'msg' => '获取成功', 'data' => $responseResult];
    }


    /**
     * 添加 编辑商品
     */
    public function addEditGoods($params)
    {
        $this->dbMaster->begin();
        $bShopGoodsDb = new BShopGoods();
        if ($params['goods_id'] > 0) {
            $bShopGoodsDb = $bShopGoodsDb->findFirstById($params['goods_id']);
            if (!$bShopGoodsDb) {
                return ['status' => false, 'msg' => '没有找到商品信息'];
            }
            $bShopGoodsDb->updated_at = date('Y-m-d H:i:s');
        } else {
            $bShopGoodsDb->created_at = date('Y-m-d H:i:s');
            $bShopGoodsDb->goods_sn = Common::order_sn("{$params['shop_id']}_");
        }
        $bShopGoodsDb->category_id = $params['category_id'][0];
        $bShopGoodsDb->category_one_id = $params['category_id'][1];
        $bShopGoodsDb->category_two_id = $params['category_id'][2];
        $bShopGoodsDb->detailed_img = implode(",", $params['detailed_img']);
        $bShopGoodsDb->banner = "";
        $bShopGoodsDb->name = $params["name"];
        $bShopGoodsDb->describe = $params['describe'];
        $bShopGoodsDb->details = $params['details'];
        $bShopGoodsDb->status = $params['status'];
        $bShopGoodsDb->model_id = $params['m_id'] ? $params['m_id'] : 0;
        $bShopGoodsDb->shop_id = $params['shop_id'];
        if (!$bShopGoodsDb->save()) {
            $this->dbMaster->rollback();
            return ['status' => false, 'msg' => '添加商品失败'];
        }
        foreach ($params['sku'] as $key=>$val) {
            $BShopGoodsSkuDb = new BShopGoodsSku();
            if (isset($val['id']) && !empty($val['id'])) {
                $BShopGoodsSkuDb = $BShopGoodsSkuDb->findFirstById($val['id']);
                if(!$BShopGoodsSkuDb){
                    $this->dbMaster->rollback();
                    return ['status' => false, 'msg' => '沒有找到商品的sku'];
                }
                $BShopGoodsSkuDb->updated_at = date('Y-m-d H:i:s');
            } else {
                $BShopGoodsSkuDb->updated_at = date('Y-m-d H:i:s');
                $BShopGoodsSkuDb->created_at = date('Y-m-d H:i:s');
            }
            $BShopGoodsSkuDb->goods_id = $bShopGoodsDb->id;
            $BShopGoodsSkuDb->value_ids = substr($val['value_ids'], 0, strlen($val['value_ids']) - 1);;
            $BShopGoodsSkuDb->image_url = $val['image_url'];
            $BShopGoodsSkuDb->stock = $val['stock'];
            $BShopGoodsSkuDb->price = $val['price'];
            $BShopGoodsSkuDb->endprice = $val['endprice'];
            $BShopGoodsSkuDb->commission = $val['commission'];
            $BShopGoodsSkuDb->is_default = ($params['sku_is_default'] == $key) ? 1 : 0;
            if (!$BShopGoodsSkuDb->save()) {
                $this->dbMaster->rollback();
                return ['status'=>false,'msg'=>'商品规格添加编辑失败'];
            }
        }
        $this->dbMaster->commit();
        return ['status'=>true,'msg'=>'添加编辑成功'];
    }


    /**
     * 模型详情
     */
    public function modelDetails($m_id, $shop_id)
    {
        $db = new BShopSkuModelKey();
        $list = $db->getDetails($m_id, $shop_id);
        return $list;
    }


    /**
     * 获取商品详情
     */
    public function onlineGoodsInfo($goods_id){
        $goods = new BShopGoods();
        $temp  = $goods->findFirstById($goods_id);
        if (!$temp) {
            return ['status'=>false,'msg'=>'商品不存在'];
        }
        $result = $temp->toarray();
        $result['goods_id'] = $result['id'];
        $result['status'] = (int)$result['status'];
        $result['model_id'] = (int)$result['model_id'];
        $result['detailed_img'] = explode(',',$result['detailed_img']);
        $result['category_id'] = [$result['category_id'],$result['category_one_id'],$result['category_two_id']];
        $skuDb         = new BShopGoodsSku();
        $sTemp         = $skuDb->find("goods_id = {$goods_id}")->toarray();
        if(count($sTemp) == 1){
            $result['price_type'] = isset($sTemp[0]['value_ids']) && !empty($sTemp[0]['value_ids']) ? 2 : 1;
        }else{
            $result['price_type'] = 2;
        }
        $valDb = new BShopSkuValue();
        foreach ($sTemp as $key=>&$val){
            $result['sku_is_default'] = 0;
            $ids = explode('_',$val['value_ids']);
            $names = [];
            foreach ($ids as $vID){
                $info = $valDb->findFirstById($vID);
                if($info){
                    $names[] = $info->value_name;
                }
            }
            if($result['sku_is_default'] == 0){
                $result['sku_is_default'] = ($val['is_default']==1) ? $key : 0;
            }
            $val['value_names'] = implode('</br>',$names);
            $val['index_key'] = $key;
        }
        $result['sku'] = $sTemp;
        return ['status'=>true,'msg'=>'获取成功','data'=>$result];
    }

}